package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.sysRole.SysRoleService;
import cn.cvs.service.sysUser.SysUserService;
import cn.cvs.service.sysUser.UserService;
import com.sun.deploy.net.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 许欣
 * 时间：2022/5/1 15:08
 * 描述：
 */
@Controller
@RequestMapping(value = "/userPwd")
public class SysUserPwd {
    @Resource
    private SysUserService sysUserService;
    @Autowired
    private UserService userService;

    /**
     * 修改密码
     * @param id 编号
     * @param newpassword 原密码
     * @param rnewpassword 新密码

     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/pwd")
    public String pwd(@RequestParam("id") Integer id, @RequestParam("newpassword") String newpassword,
                      @RequestParam("rnewpassword") String rnewpassword, Model model) throws Exception {
        if (!userService.isUserExist(id)){
            model.addAttribute("msg","用户不存在");
        }else {
            if (newpassword.equals(userService.getPasswordByUsername(id))){
                userService.modifyPasswordByUsername(id,rnewpassword);
                model.addAttribute("id",id);
                model.addAttribute("msg","修改密码成功");

            }else {
                model.addAttribute("msg","密码错误");
            }
        }
        return "/sysUser/updatePassword";
    }
}
